package com.jay.compass.dialog;

import com.jay.compass.R;
import com.jay.compass.R.id;
import com.jay.compass.R.layout;
import com.jay.compass.R.string;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendBeidouDialog extends Dialog implements TextWatcher {

	private EditText idEditText;
	private EditText contextEditText;
	private Button sendButton;
	private Button cancelButton;
	private static final int INPUT_MAX_LEN = 40;
	private Context mContext;

	public SendBeidouDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setThisDialog();
		setTitle(context.getResources().getString(
				R.string.dialog_send_beidou_title));
		mContext = context;
	}

	private void setThisDialog() {
		View mView = LayoutInflater.from(getContext()).inflate(
				R.layout.dialog_send_beidou, null);
		idEditText = (EditText) mView.findViewById(R.id.beidou_id_editText);
		contextEditText = (EditText) mView
				.findViewById(R.id.beidou_content_editText);
		contextEditText.addTextChangedListener(this);
		sendButton = (Button) mView.findViewById(R.id.dialog_send_button);
		cancelButton = (Button) mView.findViewById(R.id.dialog_cancel_button);
		super.setContentView(mView);
	}

	public void clearContent() {
		contextEditText.setText("");
	}

	public void clearId() {
		idEditText.setText("");
	}

	public void setOnSendListener(View.OnClickListener listener) {
		sendButton.setOnClickListener(listener);
	}

	public void setOnCancelListener(View.OnClickListener listener) {
		cancelButton.setOnClickListener(listener);
	}

	public String getId() {
		return idEditText.getEditableText().toString();
	}

	public String getContent() {
		return contextEditText.getEditableText().toString();
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		Editable editable = contextEditText.getText();
		int len = editable.length();
		if (len > INPUT_MAX_LEN) {
			Toast.makeText(
					mContext,
					mContext.getResources().getString(
							R.string.more_max_length_string),
					Toast.LENGTH_SHORT).show();
			int selEndIndex = Selection.getSelectionEnd(editable);
			String string = editable.toString();
			String newString = string.substring(0, INPUT_MAX_LEN);
			contextEditText.setText(newString);
			editable = contextEditText.getText();

			int newLen = editable.length();
			if (selEndIndex > newLen) {
				selEndIndex = editable.length();
			}
			Selection.setSelection(editable, selEndIndex);
		}
	}
}
